//商品移除购物车
function removeCart(goods_id, type) {
    var goods_id = parseInt(goods_id);
    $.getJSON(creatUrl("simple/removeCart"), {goods_id: goods_id, type: type}, function (content) {
        if(content.isError==false) {
            $('[name="mycart_count"]').html(content.data['count']);
            $('[name="mycart_sum"]').html(content.data['sum']);
        }else {
            alert(content.message);
        }
    });
}

//添加关注
function follow_add_ajax(user_id, obj) {
    $.getJSON(creatUrl("simple/follow_add"), {"user_id": user_id, "random": Math.random()}, function (data) {
        if(data.res) {
            $(obj).parent().html("<a href='javascript:;' onclick='follow_del_ajax("+user_id+",this)'>取消关注</a>");
        }else {
            alert(data.msg);
        }

    });
}

//取消关注
function follow_del_ajax(user_id, obj) {
    $.getJSON(creatUrl("ucenter/follow_del"), {"user_id": user_id, "random": Math.random()}, function (data) {
        if(data.res) {
            $(obj).parent().html("<a href='javascript:;' onclick='follow_add_ajax("+user_id+",this)'>关注</a>");
        }else {
            alert(data.msg);
        }

    });
}
//添加收藏夹
function favorite_add_ajax(goods_id, obj) {
    $.getJSON(creatUrl("simple/favorite_add"), {"goods_id": goods_id, "random": Math.random()}, function (content) {
        alert(content.message);
    });
}

//购物车展示
function showCart() {
    $.getJSON(creatUrl("simple/showCart"), {sign: Math.random()}, function (content) {
        var cartTemplate = template.render('cartTemplete', {'goodsData': content.data, 'goodsCount': content.count, 'goodsSum': content.sum});
        $('#div_mycart').html(cartTemplate);
        $('#div_mycart').show();
    });
}

//dom载入成功后开始操作
$(function () {
    //购物车数量加载
    if($('[name="mycart_count"]').length>0) {
        $.getJSON(creatUrl("simple/showCart"), {sign: Math.random()}, function (content) {
            $('[name="mycart_count"]').html(content.count);
        });

        //购物车div层显示和隐藏切换
        var mycartLateCall = new lateCall(200, function () {
            showCart();
        });
        $('[name="mycart"]').hover(
                function () {
                    mycartLateCall.start();
                },
                function () {
                    mycartLateCall.stop();
                    $('#div_mycart').hide('slow');
                }
        );
    }
    /**
     * 加入小组
     * @team_id
     */
    $("input[name='team_join']").click(function () {
        var obj = $(this);
        team_id = obj.attr('val');
        if(!team_id) {
            return;
        }

        $.getJSON(creatUrl("community/team_join"), {'id': team_id}, function (json) {
            if(!json.res) {
                alert(json.msg);
            }else {
                num = Number(obj.parent().find('span').html());
                obj.val('加入小组'==obj.val()?'退出小组':'加入小组');
                obj.parent().find('span').html('加入小组'==obj.val()?num-1:num+1);
            }
        });
    });
});

//[ajax]加入购物车
function joinCart_ajax(id, type) {
    $.getJSON(creatUrl("simple/joinCart"), {"goods_id": id, "type": type, "random": Math.random()}, function (content) {
        if(content.isError==false) {
            var count = parseInt($('[name="mycart_count"]').html())+1;
            $('[name="mycart_count"]').html(count);
            alert(content.message);
        }else {
            alert(content.message);
        }
    });
}

//列表页加入购物车统一接口
function joinCart_list(id) {
    $.getJSON(creatUrl("/simple/getProducts"), {"id": id}, function (content) {
        if(!content||content.length==0) {
            joinCart_ajax(id, 'goods');
        }else {
            artDialog.open(creatUrl("/block/products_list/goods_id/"+id+"/type/radio/is_products/1"), {
                id: 'selectProduct',
                title: '选择货品到购物车',
                okVal: '加入购物车',
                ok: function (iframeWin, topWin) {
                    var goodsList = $(iframeWin.document).find('input[name="id[]"]:checked');

                    //添加选中的商品
                    if(goodsList.length==0) {
                        alert('请选择要加入购物车的商品');
                        return false;
                    }
                    var temp = $.parseJSON(goodsList.attr('data'));

                    //执行处理回调
                    joinCart_ajax(temp.id, 'product');
                    return true;
                }
            })
        }
    });
}

//[首页]目的地切换
function destinationAjax(tab, city, cat) {
    $('#hexGrid').attr('vdata', city);
    $('#wrap').attr('vdata', tab);
    $('#wrap').attr('vcat', cat);
    city = parseInt(city);

    $.getJSON("/site/destinationAjax", {'tab': $('#wrap').attr('vdata'), 'city': city, 'cat': $('#wrap').attr('vcat')}, function (data) {
        if(data.res==true) {
            if(1==tab) {
                $('#profile').html(data.data);
            }else if(5==tab) {
                map.centerAndZoom(new BMap.Point(data.data.map_x, data.data.map_y), data.data.map_zoom);
                map.addControl(new BMap.MapTypeControl({mapTypes: [BMAP_NORMAL_MAP, BMAP_HYBRID_MAP]}));
                map.setCurrentCity(data.data.area_name);
                map.enableScrollWheelZoom(true);
            }else if(2==tab) {
                $('#scenery').html(data.data);
            }else if(3==tab) {
                $('#hotel').html(data.data);
            }else if(4==tab) {
                $('#food').html(data.data);
            }
        }
    });
}
